package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.ScreenListPojo;
import com.xbongbong.paas.pojo.dto.RelationClueScreenListDTO;
import com.xbongbong.paas.pojo.dto.Screen4DistributorDTO;
import com.xbongbong.paas.pojo.dto.ScreenAddDTO;
import com.xbongbong.paas.pojo.dto.ScreenListDTO;
import com.xbongbong.paas.pojo.vo.ScreenAddVO;
import com.xbongbong.paas.pojo.vo.ScreenListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.ScreenService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/9/11 10:12
 * @desrcption: 和redis交互的接口
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/screen")
public class ScreenController {

    @Resource
    private ScreenService screenService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 设置常用筛选项
     * @param screenAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenAdd(@RequestBody @Valid ScreenAddDTO screenAddDTO, BindingResult br) {
        XbbResponse<ScreenAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(screenAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(screenAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ScreenAddVO screenAddVO = screenService.add(screenAddDTO);
                response = new XbbResponse<>(screenAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenList(@RequestBody @Valid ScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.list(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取常用筛选项
     * @param screen4DistributorDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/forDistributor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenForDistributor(@RequestBody @Valid Screen4DistributorDTO screen4DistributorDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.forDistributor(screen4DistributorDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取 市场活动详情tab-关联销售线索  常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/relationClueScreenList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String relationClueScreenList(@RequestBody @Valid RelationClueScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.relationClueScreenList(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 设置数据回收站常用筛选项
     * @param screenAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/del/add/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenDelAdd(@RequestBody @Valid ScreenAddDTO screenAddDTO, BindingResult br) {
        XbbResponse<ScreenAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(screenAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(screenAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ScreenAddVO screenAddVO = screenService.delScreenAdd(screenAddDTO);
                response = new XbbResponse<>(screenAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取数据回收站常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/del/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenDelList(@RequestBody @Valid ScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.delScreenList(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置选择产品常用筛选项
     * @param screenAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectProduct/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectProductAdd(@RequestBody @Valid ScreenAddDTO screenAddDTO, BindingResult br) {
        XbbResponse<ScreenAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(screenAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(screenAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ScreenAddVO screenAddVO = screenService.selectScreenAdd(screenAddDTO);
                response = new XbbResponse<>(screenAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置待入库选择采购合同常用筛选项
     * @param screenAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectPurchase/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectPurchaseAdd(@RequestBody @Valid ScreenAddDTO screenAddDTO, BindingResult br) {
        XbbResponse<ScreenAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(screenAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(screenAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ScreenAddVO screenAddVO = screenService.screenSelectPurchaseAdd(screenAddDTO);
                response = new XbbResponse<>(screenAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置待出库选择合同常用筛选项
     * @param screenAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectContract/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectContractAdd(@RequestBody @Valid ScreenAddDTO screenAddDTO, BindingResult br) {
        XbbResponse<ScreenAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(screenAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(screenAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                ScreenAddVO screenAddVO = screenService.screenSelectContractAdd(screenAddDTO);
                response = new XbbResponse<>(screenAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取选择产品常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectProduct/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectProductList(@RequestBody @Valid ScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.selectScreenList(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取待入库选择采购合同常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectPurchase/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectPurchaseList(@RequestBody @Valid ScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.screenSelectPurchaseList(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取待出库选择合同常用筛选项
     * @param screenListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/selectContract/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenSelectContractList(@RequestBody @Valid ScreenListDTO screenListDTO, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.screenSelectContractList(screenListDTO);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 获取客户对账单常用筛选项列表
     * @param screenListPojo
     * @param br
     * @return
     */
    @RequestMapping(value = "/customerStatement/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String screenCustomerStatementList(@RequestBody @Valid ScreenListPojo screenListPojo, BindingResult br) {
        XbbResponse<ScreenListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ScreenListVO screenListVO = screenService.screenCustomerStatementList(screenListPojo);
                response = new XbbResponse<>(screenListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
